<?php
    session_start();
    require_once 'php/database.php';
    require_once 'php/photo.php';

	if (isset($_SESSION['username'])){
			$login = true;
	}
	else{
			$login = false;
	}
    function curPageURL() {
         $pageURL = 'http';
         if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
         $pageURL .= "://";
         if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
         } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
         }
         return $pageURL;
    }

    function getUserNamePage(){
        if (isset($_SESSION['username']))
	{
		$login = true;
	}
	else{
		$login = false;
		//header("Location: ".$CFG->url."home.php");
	}
	$loguname = $_SESSION['username'];
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
        return $username;
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style/footoo.css" />
    <link rel="stylesheet" type="text/css" href="style/ftheader.css" />
    <link rel="stylesheet" type="text/css" href="style/ftfooter.css" />
    <link rel="stylesheet" type="text/css" href="style/ftcommoncontent.css" />
    <link rel="stylesheet" type="text/css" href="style/ftprofmes.css" />
    <link rel="icon" href="images/footoologomini.png" type="image/png" />
    <script type="text/javascript" src="script/editprofile.js"></script>
    <script type="text/javascript" src="script/datetimepicker.js"></script>
    <title>Profile</title>
    <script type="text/javascript">
        function goToFollowingUser(user){
            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("profilecontentajax1").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","profile2.php?un="+user,true);
            xmlhttp.send();
        }

        function goToFollowerUser(user){
            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("profilecontentajax1").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","profile3.php?un="+user,true);
            xmlhttp.send();
        }

        function backToProfile(user){
            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("profilecontentajax1").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","profile4.php?un="+user,true);
            xmlhttp.send();
        }

        function toEditProfile(user){
            //document.location = "edit_profile.php?un="+user;

            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("profilecontentajax1").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","edit_profile1.php?un="+user,true);
            xmlhttp.send();
        }

        function toWall(user){
            document.location = "wall.php?"+user;
        }

        function toPhotos(user){
            document.location = "fts.php?un="+user;
        }

        function submiteditform(){
            document.forms["editprofile"].submit();
        }

    </script>
</head>

<body>
    <div id="headerwrapper">
	<div id = "headerbgfx"></div>
        <div id="footooheader">
            <div id="footoologo">
            	<a href="#"></a>
            </div>
            <div id="profpicarea">
            <?php require 'php/viewer/header.php'; ?>
            </div>
        </div>
        <div id="footoomenu">
            <ul>
                <li>
                    <a href="home.php">Home</a>
                </li>
                <li>
                    <?php
                        echo '<a href="profile.php?un='.$_SESSION['username'].'" class="current">Profile</a>';
                    ?>
                </li>
                <li>
                    <?php
                        echo '<a href="wall.php?'.$_SESSION['username'].'">Wall</a>';
                    ?>
                </li>
                <li>
                    <?php
                        echo '<a href="fts.php?un='.$_SESSION['username'].'" >Photos</a>';
                    ?>
                </li>
                <li>
                    <?php
                        echo '<a href="message.php?un='.$_SESSION['username'].'" >Message</a>';
                    ?>
                </li>
            </ul>
        </div>
    </div>
    <div id="headermirror">
    	<div id="searchbox">
            <form action="search.php" method="post">
            	<select class="searchselect" name="tipe">
                	<option value="user">User</option>
                	<option value="photo">Photo</option>
                </select>
                <div class="searchbutton">
                    <input id="searchbutton" type="submit" value="" />
                </div>
                <div class="searchtext">
                <input id="searchtext" class="searchtext" type="text" name="searchtext" value="search" onfocus="filledTrue(this.id, 'search')" onblur="filledFalse(this.id, 'search')" />
            	</div>
            </form>		
        </div>
    </div>
    <div id="contentwrapper">
        <?php
            $user = getUserNamePage();

            $result1 = database::query("SELECT HakAksesWall FROM user WHERE Username = '$user'");
            if(mysql_num_rows($result1)>0){
                $bar1 = mysql_fetch_row($result1);
                $akseswall = $bar1[0];
                $role = $bar1[1];
            }
            
            $seorangfollower = false;
            if ($login){
                $login_user = $_SESSION["username"];
                $result2 = database::query("SELECT Follower FROM user WHERE Username = '$user'");
                if(mysql_num_rows($result1)>0){
                    while($bar1 = mysql_fetch_row($result1)){
                        $foll = $bar1[0];
                        if ($foll == $login_user){
                            $seorangfollower = $seorangfollower || true;
                        }
                        else{
                            $seorangfollower = $seorangfollower || false;
                        }
                    }
                }
            }
			if ($login_user==$user){
				$hak = true;
			}
            else if ($seorangfollower && ($akseswall == 1)){
                $hak = true;
            }
            else if (($akseswall == 2) && $login){
                $hak = true;
            }
            else if (($akseswall == 3)){
                $hak = true;
            }
            else if ($role == 1){
                $hak = true;
            }
            else {
                $hak = false;
            }

            if (!$hak){
                echo '<p align="center"><h1>MAAF, ANDA TIDAK DIIZINKAN MELIHAT PROFILE INI.</h1></p>';
            }
            else {

        ?>
        <div class="boxy">
            <div id="profilecontentajax1" class="profilecontent">
                <table>
                    <tr>
                        <?php
                        echo '<td rowspan="2" valign="middle">';
                            echo '<div id="profileinfocontent">';
                                    $user = getUserNamePage();
                                    $hasil = database::query("SELECT * FROM user WHERE Username = '$user'");
                                    $count = 0;

                                    if(mysql_num_rows($hasil)>0){
                                        while ($baris = mysql_fetch_row($hasil)){
                                            $nama = $baris[3];
                                            $email = $baris[4];
                                            $tanggal = $baris[5];

                                            $gambar = photo::getProfPicPath($user);

                                            /*if ($baris[8] == NULL){
                                                $gambar = 'images/bg-photo.png';
                                            }
                                            else{
                                                $idgambar = $baris[8];
                                                $hasil2 = database::query("SELECT * FROM Foto WHERE IDFOTO = $idgambar");
                                                if(mysql_num_rows($hasil2)>0){
                                                    $baris2 = mysql_fetch_row($hasil2);
                                                    $gambar= 'users/'.$baris2[1].'/'.$baris2[2];
                                                }
                                            }*/

                                            echo '<div id="fotopropic" class="boxy">';
                                                echo '<img src="'.$gambar.'" alt="Foto Profil disini" id="foto_profil" name="foto_profil" width="100" height="100"/>';
                                            echo '</div>';
                                        }
                                    }
                                
                            echo '</div>';
                        echo '</td>';
                        echo '<td rowspan="2" valign="middle" width="400">';
                            echo '<div class="profile_info">';
                                echo '<div>';
                                    echo '<label class="profileinfo_label" id="_name">Name</label>';
                                    echo '<label class="profileinfo_value" id="name">'.$nama.'</label>';
                                echo '</div>';
                                echo '<div>';
                                    echo '<label class="profileinfo_label" id="_email">Email</label>';
                                    echo '<label class="profileinfo_value" id="email">'.$email.'</label>';
                                echo '</div>';
                                echo '<div>';
                                    echo '<label class="profileinfo_label" id="_tanggal_lahir">Date of Birth</label>';
                                    echo '<label class="profileinfo_value" id="tanggal_lahir">'.$tanggal.'</label>';
                                echo '</div>';
                            echo '</div>';
                        echo '</td>';
                        ?>
                        <td>
                            <div>
                                <label>Following</label>
                                <?php

                                    echo '<div>';
                                            //echo '<div  class="followinger">';
                                            echo '<div>';
                                    if (isset($_SESSION['username'])){
                                            $login = true;
                                    }
                                    else{
                                            $login = false;
                                    }
                                    $user = getUserNamePage();

                                    $hasil = database::query("SELECT Username FROM follownet WHERE Follower = '$user'");
                                    $indeks = 0;
                                    $count = 0;
                                    if(mysql_num_rows($hasil)>0){
                                        while($baris = mysql_fetch_row($hasil)){
                                            /*if ($baris[8] == NULL){
                                                $gambar = 'images/bg-photo.png';
                                            }
                                            else{
                                                $idgambar = $baris[8];
                                                $hasil2 = database::query("SELECT * FROM Foto WHERE IDFOTO = $idgambar");
                                                $baris2 = mysql_fetch_row($hasil2);
                                                $gambar = 'users/'.$baris2[1].'/'.$baris2[2];
                                            }*/
                                            $pemilik = $baris[0];
                                            $gambar = photo::getProfPicPath($pemilik);
                                            $namaArray[$indeks] = $baris[3];
                                            $pemilikArray[$indeks] = $pemilik;
                                            $gambarArray[$indeks] = $gambar;
                                            ++$indeks;
                                            ++$count;
                                        }

                                        for ($j = 0; $j < 3; ++$j){
                                                echo '&nbsp';
                                                echo '<a title="'.$namaArray[$j].'" href="profile.php?un='.$pemilikArray[$j].'"><img class="boxy2" src="'.$gambarArray[$j].'" alt="fol_'.$j.'" width="50" height="50" name="foto_fol_'.$j.'" id="foto_fol_'.$j.'"/></a>';
                                        }

                                                echo '&nbsp';
                                            echo '</div>';
                                        if ($count == 0){
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>&nbsp';
                                        }
                                    }

                                    echo '</div>';
                                    if ($indeks > 3){
                                        //echo '<br/><a href="">See all...</a>';
                                        echo '<br/><a href="javascript:void()" onclick="goToFollowingUser(\''.$user.'\')">See all..<br/></a>';
                                    }
                                ?>
                                <br/>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div>
                                <br/>
                                <label>Follower</label>
                                <?php

                                    echo '<div>';
                                            //echo '<div  class="followinger">';
                                            echo '<div>';
                                    if (isset($_SESSION['username'])){
                                            $login = true;
                                    }
                                    else{
                                            $login = false;
                                    }
                                    $user = getUserNamePage();

                                    $hasil = database::query("SELECT Follower FROM follownet WHERE Username = '$user' LIMIT 4 ");
                                    $indeks = 0;
                                    $count = 0;
                                    if(mysql_num_rows($hasil)>0){
                                        while($baris = mysql_fetch_row($hasil)){
                                            /*if ($baris[8] == NULL){
                                                $gambar = 'images/bg-photo.png';
                                            }
                                            else{
                                                $idgambar = $baris[8];
                                                $hasil2 = database::query("SELECT * FROM Foto WHERE IDFOTO = $idgambar");
                                                $baris2 = mysql_fetch_row($hasil2);
                                                $gambar = 'users/'.$baris2[1].'/'.$baris2[2];
                                            }*/
                                            $pemilik = $baris[0];
                                            $gambar = photo::getProfPicPath($pemilik);
                                            $namaArray2[$indeks] = $baris[3];
                                            $pemilikArray2[$indeks] = $pemilik;
                                            $gambarArray2[$indeks] = $gambar;
                                                
                                            ++$indeks;
                                            ++$count;
                                        }

                                        for ($j = 0; $j < 3; ++$j){
                                            if($j < $indeks){
                                                echo '&nbsp';
                                                echo '<a title="'.$namaArray2[$j].'" href="profile.php?un='.$pemilikArray2[$j].'"><img class="boxy2" src="'.$gambarArray2[$j].'" alt="fol_'.$j.'" width="50" height="50" name="foto_fol_'.$j.'" id="foto_fol_'.$j.'"/></a>';
                                            }
                                        }

                                                echo '&nbsp';
                                            echo '</div>';
                                        if ($count == 0){
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>';
                                            echo '<br/>&nbsp';
                                        }
                                    }

                                    echo '</div>';
                                    if ($indeks > 3){
                                        //echo '<br/><a href="">See all...</a>';
                                        echo '<br/><a href="javascript:void()" onclick="goToFollowerUser(\''.$user.'\')">See all..<br/></a>';
                                    }
                                ?>
                            </div>
                        </td>
                    </tr>
                </table>
                <div id="linkfromprofile">
                    <?php
                        if (isset($_SESSION['username'])){
                                $login = true;
                        }
                        else{
                                $login = false;
                        }
                        $user = getUserNamePage();
                        if ($login){
                            if ($user == $_SESSION['username']){
                                echo '<div>';
                                //echo '    <a href="edit_profile.php?un='.$user.'">Edit Profile</a>';
                                echo '<div  class="button180" onclick="toEditProfile(\''.$user.'\')">Edit Profile</div>';
                                echo '</div>';
                            }
                        }
                        echo '<div>';
                            //echo '<a href="wall.php?'.$user.'">Wall</a>';
                            echo '<div  class="button180" onclick="toWall(\''.$user.'\')">Wall</div>';
                        echo '</div>';
                        echo '<div>';
                            //echo '<a href="fts.php?un='.$user.'">Photos</a>';
                            echo '<div  class="button180" onclick="toPhotos(\''.$user.'\')">Photos</div>';
                        echo '</div>';

                    ?>
                </div>
            </div>
        </div>
        <?php
            }
        ?>
    </div>
    <div id="footerwrapper">
    	<div id="footer">
            <div id="footerleft"><br/>
            	ft, ft logoes are trademark of ft company. <br/>
            	Copyright 2010 by : <br/>
                13508011 - M. Albadr Lutan Nasution <br/>
                13508019 - Gregorius Ronny Kaluge <br/>
                13508099 - Alwi Alfiansyah Ramdan <br/>
            </div>
            <div id="footermiddle">

            </div>
            <div id="footerright">

            </div>
        </div>
    </div>
</body>
</html>
